var FeedBack = {
    showOrHide: function(){
        var f = $("#feedback");
        if(f.css('display')=='none'){
            f.show();
            $("#feedbackBtn").css('right', '224px').find('img').css('margin-right', '7px');
        }else{
            f.hide();
            $("#feedbackBtn").css('right', '0px').find('img').css('margin-right', '-10px');
        }
    },
    send: function(){
        var c = $("#feedbackContent").val();
        if(c){
            var movieId = '';
            if($("#movie_id").length){
                movieId = $("#movie_id").val();
            }
            $("#feedbackContent, #btnSendFeedback").attr("disabled", true);
            $.ajax({
                url: '/feedback/json/',
                type: 'post',
                dataType: 'json',
                data: {content:c, movie_id:movieId },
                success: function(data){
                    if(data && data.success){
                        alert('反馈成功，谢谢您的反馈。');
                        FeedBack.showOrHide();
                    }else{
                        alert('很抱歉，提交失败，请重试');
                    }
                },
                error: function(){
                    alert('很抱歉，提交失败，请重试');
                },
                complete: function(){
                    $("#feedbackContent, #btnSendFeedback").attr("disabled", false);
                }
            });
        }else{
            alert('请输入反馈内容');
        }
    },
    scroll: function(){
        var b = $("#feedbackBtn"), f = $("#feedback");
        //var offset = b.offset();
        var st = $(document).scrollTop();
        f.css('top', (280+st)+'px');
        b.css('top', (300+st)+'px');
    }
};

var Search = {
    showTip: function(){
        var q = $("#s_q");
        if($.trim(q.val())==''){
            q.css('color','#ccc').val(q.attr('title'));
        }
    },
    hideTip: function(){
        var q = $("#s_q");
        if($.trim(q.val())==q.attr('title')){
            q.val('').css('color','#000');
        }
    },
    submit: function(){
        var q = $("#s_q");
        var v = $.trim(q.val());
        if(v==q.attr('title') || v==''){
            return false;
        }
        return true;
    }
};

// 获取当前用户id，若用户未登录，则返回null
function get_user_id() {
	// <input type="hidden" value="{{user_info.id}}" id="user_id" />
	var user_id = $('#user_id').val();
	return user_id == '' ? null : user_id;
}

jQuery.ajaxSetup({cache: false});

$(document).ready(function(){
    $(window).scroll(function(){
        FeedBack.scroll();
    });
    
    // 处理静态页面的登录状态
    // <input type="hidden" value="1|0" id="is_dynamic" />
    var is_dynamic = $('#is_dynamic').val() == '1';
    if(!is_dynamic) {
    	var url = '/current_user';
    	$.getJSON(url, function(user_info) {
    		if(user_info) {
    			var tpl = '<?js var screen_name_link = get_screen_name_link(user_info, false); ?>\n\
    			<span class="login"><img src="/common/images/tsina.gif" /> <a href="/logout">登出(logout)</a></span>\
				<span class="userinfo">\
					<img style="width:18px;height:18px;" title="${user_info.description}" src="${user_info.profile_image_url}" />\
					<span>#{screen_name_link}</span>\
				</span>';
				var html = Shotenjin.render(tpl, {'user_info': user_info});
				$('#userform').html(html);
    		}
    	});
    }
});